package reflect1;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Test {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class<?> clazz = Class.forName("reflect1.Stu");
        Constructor<?>[] constructors = clazz.getDeclaredConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.println(constructor);
        }

        // 获取空参构造
        Constructor<?> declaredConstructor = clazz.getDeclaredConstructor();
        System.out.println(declaredConstructor);

        // 获取带参数构造
        Constructor<?> declaredConstructor1 = clazz.getDeclaredConstructor(String.class);
        System.out.println(declaredConstructor1);

        // 获取权限修饰符
        int modifiers = declaredConstructor1.getModifiers();
        System.out.println(modifiers);

        // 获取参数个数
        System.out.println(declaredConstructor1.getParameterCount());

        // 创建对象
        declaredConstructor1.setAccessible(true); //临时取消权限校验
        Stu o = (Stu) declaredConstructor1.newInstance("sfsd");
        System.out.println(o);
    }
}
